package com.sise.gathering;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;

/**
 * @author idea
 * @data 2020/4/8
 */
public class GatheringDemo {

    public static void main(String[] args) throws IOException {
        ByteBuffer header = ByteBuffer.allocate(128);
        ByteBuffer body = ByteBuffer.allocate(1024);

        RandomAccessFile randomAccessFile = new RandomAccessFile("test.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        System.out.println("采集 Gathering 操作");
        Channel channel = randomAccessFile.getChannel();
        ByteBuffer[] byteBuffers = new ByteBuffer[]{header,body};
        ((FileChannel) channel).read(byteBuffers);

        System.out.println("散射 Scattering 操作");
        ((FileChannel) channel).write(byteBuffers);
    }
}
